package org.nidy.programexercise.Exception;

import org.nidy.programexercise.MethodCitation.Student;

public class ExceptionDemo {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        try {
            System.out.println(arr[9]);
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("数组越界了");
            e.printStackTrace();
        }
        System.out.println("执行了吗？");

        System.out.println("============================================");
        int[] arr1={1,2,3,4,5,6,7,8,9,10};
        try {
            getMax(arr1);
        } catch (NullPointerException e) {
            System.out.println("数组为空");
            e.printStackTrace();
        }catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("数组越界了");
            e.printStackTrace();
        }
        Student student = new Student();
        try {
            student.setAge(180);
            student.setName("nidy");
        } catch (CustomException e) {
            System.out.println("自定义异常");
            e.printStackTrace();
        }


    }
    public static int getMax(int[]arr){
        if (arr.length==0){
            throw new NullPointerException("数组为空");
        }
        int max=arr[0];
        for (int i = 1; i < arr.length; i++) {
            if (arr[i]>max){
                max=arr[i];
            }
        }
        return max;
    }
}
